package org.webball.pitch;

import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.webball.base.abstracts.AbstractBall;
import org.webball.base.interfaces.IController;
import org.webball.controller.Controller;
import org.webball.draw.Draw;
import org.webball.tools.MKVector;

import org.webball.player.Player;

/**
 * 
 * @author Maciej Koch
 *
 */
public class Test extends BasicGame{
	
	private Pitch pitch;
	private Player checkedPlayer;
	private boolean checked = false;
	
	IController controller;
	
	public Test(String title) {
		super(title);
    }	
    public void render(GameContainer container, Graphics g) throws SlickException {	
	
    	Draw d = new Draw(0);					
    	d.draw(g, pitch);
    }

	public void init(GameContainer container) throws SlickException {
		
		pitch = new Pitch();
		Player[] players1 = new Player[6];
		Player[] players2 = new Player[6];
		for(int i = 0 ; i < 6 ; i++) {
			players1[i] = new Player();
			players2[i] = new Player();
		}
		pitch.setTeam(0, new Team(players1));
		pitch.setTeam(1, new Team(players2));
		
		controller = new Controller();
		controller.setTeam(0);
	}	
	public void update(GameContainer container, int delta)
			throws SlickException {
		
		pitch.update();
		
		Input inp = container.getInput();
		controller.action(pitch, inp);
//		MKVector mp = new MKVector(inp.getMouseX(), inp.getMouseY());
//		
//		//faza taktyczna
//		if(pitch.isTacticalPhase()) {	
//			if(!checked) {
//				if(inp.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
//					checkedPlayer = (Player) pitch.clickOnPlayer(mp, 0);
//					if(checkedPlayer != null) {
//						checked = true;
//					}
//				}
//			}
//			if(checked) {
//				if(inp.isMousePressed(Input.MOUSE_LEFT_BUTTON) && !inp.isKeyDown(Input.KEY_SPACE)) {
//					checkedPlayer.setPosition(mp);
//				}
//				else if(inp.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) && inp.isKeyDown(Input.KEY_SPACE)) {
//					checkedPlayer.setHitTarget(mp);
//				}				
//			}
//				
//		}
//		//normalna faza
//		else {
//			if(!checked) {
//				if(inp.isMousePressed(Input.MOUSE_LEFT_BUTTON)) {
//					checkedPlayer = (Player) pitch.clickOnPlayer(mp, 0);
//					if(checkedPlayer != null) {
//						checked = true;
//					}
//				}
//			}
//			if(checked)
//				if(inp.isMousePressed(Input.MOUSE_LEFT_BUTTON) && !inp.isKeyDown(Input.KEY_SPACE)) {
//					checkedPlayer.setMovementTarget(mp);
//				}
//				else if(inp.isMouseButtonDown(Input.MOUSE_LEFT_BUTTON) && inp.isKeyDown(Input.KEY_SPACE)) {
//					checkedPlayer.setHitTarget(mp);
//				}
//		}
//		if(inp.isMousePressed(Input.MOUSE_RIGHT_BUTTON)) {
//			checked = false;
//			pitch.deselectAllPlayers(0);
//		}
	}
	public static void main(String[] args) throws SlickException {
		AppGameContainer app = new AppGameContainer(new Test("Webball"));

        app.setMaximumLogicUpdateInterval(10);
        app.setMinimumLogicUpdateInterval(50);
        app.setVSync(true);
        app.setTargetFrameRate(35);                
        app.setDisplayMode(800, 600, false);
        app.start();
}
	
}
